package com.project2.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project2.bean.CommonData;
import com.project2.bean.PageBo;
import com.project2.bean.po.MarketLog;
import com.project2.bean.po.MarketLogExample;
import com.project2.mapper.MarketLogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author : bunnyupup
 * @description :
 * @date : Created in 2022/8/14 10:20
 */
@Service
public class MarketLogServiceImpl implements MarketLogService {
    @Autowired
    MarketLogMapper marketLogMapper;

    @Override
    public CommonData<MarketLog> list(String name, PageBo info) {
        // 分页
        PageHelper.startPage(info.getPage(), info.getLimit());
//        // example
//        MarketLogExample example = new MarketLogExample();
//        MarketLogExample.Criteria criteria = example.createCriteria();
//        if (name != null && !"".equals(name)) {
//            criteria.andAdminEqualTo("%" + name + "%");
//        }
//        // 设定排序
//        example.setOrderByClause(info.getSort()+" "+info.getOrder());
//        // 查询
//        List<MarketLog> marketLogs = marketLogMapper.selectByExample(example);
        String sort1 = info.getSort();
        String order1 = info.getOrder();
        if (name != null && !"".equals(name)){
            List<MarketLog> marketLogs = marketLogMapper.queryLogByCondition(name, order1, sort1);
            PageInfo<MarketLog> pageInfo = new PageInfo<>(marketLogs);
            return CommonData.data(pageInfo);
        }
        List<MarketLog> marketLogs = marketLogMapper.queryAllLogs(order1, sort1);

        // 分页结果
        PageInfo<MarketLog> pageInfo = new PageInfo<>(marketLogs);
        return CommonData.data(pageInfo);
    }

    @Override
    public int saveLog(MarketLog marketLog) {
        int affectedRows = marketLogMapper.insertSelective(marketLog);
//        int affectedRows = marketLogMapper.insert(marketLog);
        return affectedRows;
    }

}
